<?php

namespace app\controllers;

use app\models\Category;
use app\models\Page;
use app\models\Project;
use Yii;

class DocsController extends BasisController
{
    /**
     * 文档列表页面
     * @return string
     */
    public function actionIndex()
    {
        $projectId = Yii::$app->request->get('projectId','');

        if (empty($projectId)) throw new \Exception('参数错误，非法请求');

        $projectInfo = Project::find()->where(['id' => $projectId])->asArray()->one();

        $category = Category::getCategoryByWhere(['project_id' => $projectId]);

        // 获取所有的一级分类
        $baseCate = [];
        if (!empty($category)){
            $baseCate = array_filter($category,function ($v){
                return $v['pid'] == 0;
            });
        }

        // 只有项目创建者才有管理权限，其他人只有阅读权限
        $hasAuth = isset($projectInfo['created_uid']) && (Yii::$app->user->id == $projectInfo['created_uid']);

        if (!empty($category))$category = Category::formatCategory($category);

        return $this->render('index',compact('category','projectInfo','detail','baseCate','hasAuth'));
    }

    # 获取单页详情
    public function actionPage()
    {
        if (!Yii::$app->request->isAjax)return $this->ajaxReturn(['e' => '1','m' => '请求方式错误']);
        $pageId = Yii::$app->request->get('pageId','');
        $projectId = Yii::$app->request->get('projectId','');

        // 分享链接生成
        $share['Project'] = Yii::$app->urlManager->createAbsoluteUrl(['share/index','source' => 'share','type' => 'project','projectId' => $projectId]);
        $share['Page'] = Yii::$app->urlManager->createAbsoluteUrl(['share/index','source' => 'share','type' => 'page','pageId' => $pageId]);


        $detail = !empty($pageId) ? Page::find()->where(['id' => $pageId])->asArray()->one() : [];
        $this->ajaxReturn(['e' => '0','m' => '','d' => array_merge($detail,$share)]);
    }

    #编辑添加文档
    public function actionOperate()
    {
        $pageId = Yii::$app->request->get('pageId','');
        $projectId = Yii::$app->request->get('projectId','');

        if (empty($projectId)) throw new \Exception("参数错误，非法请求");

        $projectInfo = Project::find()->where(['id' => $projectId])->asArray()->one();
        $secondCate = Category::find()->where(['pid' => '0','project_id' => $projectId])->asArray()->all();

        $thirdCate = [];
        if (!empty($secondCate)){
            foreach ($secondCate as $v){
                $thirdCate[$v['id']] = Category::find()->where(['pid' => $v['id'],'project_id' => $projectId])->asArray()->all();
            }
        }

        $detail = empty($pageId) ? [] : Page::find()->where(['id' => $pageId])->asArray()->one();

        if (!empty($detail)){
            $parentId = Category::parentId($detail['cate_id']);
        }

        return $this->render('operate',compact('parentId','projectInfo','projectId','detail','secondCate','thirdCate'));
    }

    # 保存信息
    public function actionSave()
    {
        $post = Yii::$app->request->post();

        if (empty($post))$this->ajaxReturn(['e' => '1','m' => '提交的数据不能为空']);
        $saveObj = isset($post['id']) ? Page::find()->where(['id' => $post['id']])->one() : new Page();

        $saveData = array_filter($post,function ($k){
            return in_array($k,['content','page_title','sort','cate_id','project_id','status','created_uid']);
        },ARRAY_FILTER_USE_KEY);

        $saveData['created_uid'] = 0;//Yii::$app->user->id;
        $saveData['created_time'] = time();

        $saveObj->attributes = $saveData;
        if ($saveObj->validate() && $saveObj->save()){
            $this->ajaxReturn(['e' => '0' , 'm' => '恭喜您，操作成功']);
        } else {
            $this->ajaxReturn(['e' => '1' , 'm' => '很遗憾，操作失败','d' => $saveObj->getErrors()]);
        }
    }

    public function ajaxReturn($array)
    {
        echo json_encode($array,JSON_UNESCAPED_UNICODE);
        Yii::$app->end();
    }
}
